from tensorflow.contrib.learn.python.learn.datasets import base
import numpy as np
import tensorflow as tf

tf.logging.set_verbosity(tf.logging.INFO)
#data files
IRIS_TRAINING = "iris.csv"
IRIS_TEST = "iris-test.csv"

#load files
training_set = base.load_csv_without_header(filename=IRIS_TRAINING,
                                            features_dtype=np.float32,
                                            target_dtype=np.int)

test_set = base.load_csv_without_header(filename=IRIS_TEST,
                                            features_dtype=np.float32,
                                            target_dtype=np.int)
print(training_set)
print(test_set)

# 模型
f_name = "f_name"
f_columns = [tf.feature_column.numeric_column(f_name, shape=[4])]

classifier = tf.estimator.LinearClassifier(feature_columns=f_columns,
                                           n_classes=3,
                                           model_dir="/tmp/iris/model")

# 定义输入函数
def input_fn(dataset):
    def _fn():
        features = {f_name: tf.constant(dataset.data)}
        label = tf.constant(dataset.target)
        return features, label
    return _fn

print(input_fn(training_set)())

# 训练模型
classifier.train(input_fn=input_fn(training_set), steps=1000)
print('fit done')

# 评估准确度
accuracy_score = classifier.evaluate(input_fn=input_fn(test_set), steps=100)["accuracy"]
print('\nAccuracy:{0:f}'.format(accuracy_score))